﻿using System;
using Microsoft.Win32; 
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.DataSourcesGDB;

namespace NcgmpDataAccess.Utilities
{
    public class commonFunctions
    {
        public static ITable OpenTable(IWorkspace TheWorkspace, string TableName)
        {
             IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)TheWorkspace;
            string theTable = QualifyClassName(TheWorkspace, TableName);

            return featureWorkspace.OpenTable(theTable);
        }

        public static IFeatureClass OpenFeatureClass(IWorkspace TheWorkspace, string FeatureClassName)
        {
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)TheWorkspace;
            string theFC = QualifyClassName(TheWorkspace, FeatureClassName);

            return featureWorkspace.OpenFeatureClass(theFC);
        }

        public static IRelationshipClass OpenRelationshipClass(IWorkspace TheWorkspace, string RelationshipClassName)
        {
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)TheWorkspace;
            return featureWorkspace.OpenRelationshipClass(RelationshipClassName);
        }

        public static ITopology OpenTopology(IWorkspace TheWorkspace, string TopologyName)
        {
            ITopologyWorkspace topoWorkspace = (ITopologyWorkspace)TheWorkspace;
            return topoWorkspace.OpenTopology(TopologyName);
        }

        public static IRepresentationWorkspaceExtension GetRepExtension(IWorkspace Workspace)
        {
            IWorkspaceExtensionManager ExtManager = (IWorkspaceExtensionManager)Workspace;
            UID theUID = new UIDClass();
            theUID.Value = "{FD05270A-8E0B-4823-9DEE-F149347C32B6}";
            return (IRepresentationWorkspaceExtension)ExtManager.FindExtension(theUID);
        }

        public static IRepresentationClass GetRepClass(IWorkspace Workspace, string RepresentationClassName)
        {
            // Get the RepresentationWorkspaceExtension from the Workspace
            IRepresentationWorkspaceExtension RepWsExt = GetRepExtension(Workspace);

            // Get and return the RepresentationClass
            return RepWsExt.OpenRepresentationClass(RepresentationClassName);
        }

        private static string QualifyClassName(IWorkspace theWorkspace, string givenClassName)
        {
            if (theWorkspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
            {
                string dbName = ((IDataset)theWorkspace).Name;

                // ------- TROUBLE ---------
                // I don't know how to handle situations where the owner is not DBO.
                // ------- TROUBLE ---------
                string owner = "DBO";

                ISQLSyntax Qualifier = (ISQLSyntax)theWorkspace;
                return Qualifier.QualifyTableName(dbName, owner, givenClassName);
            }
            else { return givenClassName; }            
        }

        public static IGxObject OpenArcFile(IGxObjectFilter objectFilter, string Caption)
        {
            IGxDialog fileChooser = new GxDialogClass();
            IEnumGxObject chosenFiles = null;

            fileChooser.Title = Caption;
            fileChooser.ButtonCaption = "Select";
            fileChooser.AllowMultiSelect = false;
            fileChooser.ObjectFilter = objectFilter;
            fileChooser.DoModalOpen(0, out chosenFiles);

            chosenFiles.Reset();
            return chosenFiles.Next();
        }

        public static IWorkspace OpenWorkspace(string defaultPath = null)
        {
            string pathToOpen = defaultPath;
            IWorkspaceFactory wsFact = new FileGDBWorkspaceFactoryClass();
            IWorkspace openedWorkspace;

            if (pathToOpen == null)
            {
                // Browse for a file, personal or SDE geodatabase
                IGxObjectFilter objectFilter = new GxFilterWorkspaces();
                IGxObject openedObject = commonFunctions.OpenArcFile(objectFilter, "Please select an NCGMP database");
                if (openedObject == null) { return null; }

                // Check to see if it is a File, Personal or SDE database, create appropriate workspace factory
                switch (openedObject.Category)
                {
                    case "Personal Geodatabase":
                        wsFact = new AccessWorkspaceFactoryClass();
                        pathToOpen = openedObject.FullName;
                        break;
                    case "File Geodatabase":
                        wsFact = new FileGDBWorkspaceFactoryClass();
                        pathToOpen = openedObject.FullName;
                        break;
                    case "Spatial Database Connection":
                        wsFact = new SdeWorkspaceFactoryClass();
                        IGxRemoteDatabaseFolder remoteDatabaseFolder = (IGxRemoteDatabaseFolder)openedObject.Parent;
                        pathToOpen = remoteDatabaseFolder.Path + openedObject.Name;
                        break;
                    default:
                        break;
                }
            }
            
            // This doesn't seem to be working for SDE databases.
            openedWorkspace = wsFact.OpenFromFile(pathToOpen, 0);
            return openedWorkspace;
        }
    }
}
